package ngat.oss.simulation;

import java.util.*;

import ngat.phase2.*;

public class EnhancedExecutionHistoryModel implements ExecutionHistoryModel {

    /** Model to be enhanced.*/
    private ExecutionHistoryModel baseModel;

     /** Stores latest temporary history entries.*/
    private Map tempHistory;

    /** Create an EnhancedExecutionHistoryModel on the supplied base model.*/
    public EnhancedExecutionHistoryModel(ExecutionHistoryModel baseModel) {
	this.baseModel = baseModel;
	tempHistory = new HashMap();
    }

    public void showExecutionStatistics(Group group) {
        // if we have an TEMP entry for the group we return it else return the basemodel result.
	//System.err.println("EXHM.showExecutionStatistics("+group.getName()+")");

        if (tempHistory.containsKey(group.getFullPath())) {

            List ghist = (List)tempHistory.get(group.getFullPath());
            int count = ghist.size();
	    System.err.println("EXHM: Temp history contains: "+count+" entries");
            long lastDone = 0L;
            if (count == 0)
                lastDone = 0L;
            else
                lastDone = ((Long)ghist.get(count-1)).longValue();

	    int cc = 0;
	    Iterator ih = ghist.iterator();
            while (ih.hasNext()) {
                Long h = (Long)ih.next();
		cc++;
		System.err.println("EXHM: Temp exec "+cc+" at: "+
				   ScheduleSimulator.sdf.format(new Date(h.longValue())));
	    }
	    
        } else {
	    System.err.println("EXHM: No temp executions using basemodel");
            
	}

    }


     /** Returns the execution statistics for the specified group.*/
    public ExecutionStatistics getExecutionStatistics(Group group) {
	// if we have an TEMP entry for the group we return it else return the basemodel result.

	if (tempHistory.containsKey(group.getFullPath())) {
	    List ghist = (List)tempHistory.get(group.getFullPath());	   
	    int count = ghist.size();
	    long lastDone = 0L;
	    if (count == 0)
		lastDone = 0L;
	    else
		lastDone = ((Long)ghist.get(count-1)).longValue();
	    
	    return new ExecutionStatistics(lastDone, count);

	} else 
	
	    return baseModel.getExecutionStatistics(group);
	
    
    }

    /** Returns the execution statistics for the specified group at time.*/
    public ExecutionStatistics getExecutionStatistics(Group group, long time) {
	// if we have an TEMP entry for the group we return it else return the basemodel result.
	
	if (tempHistory.containsKey(group.getFullPath())) {
	    List ghist = (List)tempHistory.get(group.getFullPath());
	   
	    int count = 0;
	    long lastDone = 0L;
	    Iterator ih = ghist.iterator();
	    while (ih.hasNext()) {
		Long h = (Long)ih.next();
		if (h.longValue() < time) {
		    count++;
		    lastDone = h.longValue();
		}
	    }
	
	    return new ExecutionStatistics(lastDone, count);

	} else 

	    return baseModel.getExecutionStatistics(group, time);

    }
	
    /** Add a temporary history entry for group at time.*/
    public void updateTemporaryHistory(Group group, long lastExecuted) {
	List ghist = (List)tempHistory.get(group.getFullPath());
	if (ghist == null) {
	    ghist = new Vector();
	    tempHistory.put(group.getFullPath(), ghist);
	    //System.err.println("EXHM: Create temp history entry for: "+group.getName());
	}
	ghist.add(new Long(lastExecuted));
	//System.err.println("EXHM: Add temp update "+ghist.size()+" for: "+group.getName()+" at "+ScheduleSimulator.sdf.format(new Date(lastExecuted)));
    }
    
    /** Clear out all temporary history entries.*/
    public void clearTemporaryHistory() {
	// iterate round zapping all the entries first then clear the map out
	// TODO
	//	Iterator ig = tempHistory.keySet().iterator();
	//while (ig.hasNext()) {
	//  String gpath = (String)ig.next();
	//  List ghist = (List)tempHistory.get(gpath);
	//  ghist.clear();
	//  ghist = null;
	//}	    
	tempHistory.clear();
    }

}
